﻿using Furion.CMS.Application.Dto;
using Furion.CMS.Core;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Furion.CMS.Application
{
    /// <summary>
    /// 登录类型常量
    /// </summary>
    public static class LoginTypeConsts
    {
        /// <summary>
        /// 账号
        /// </summary>
        public const string Account = "account";
        /// <summary>
        /// 手机号
        /// </summary>
        public const string Mobile = "mobile";
    }

    public class LoginVerifyType : Enumeration<int>
    {
        /// <summary>
        /// 普通账号
        /// </summary>
        public static readonly LoginVerifyType Account = new(1, LoginTypeConsts.Account, "username", "password", "账户或密码错误");
        /// <summary>
        /// 手机号
        /// </summary>
        public static readonly LoginVerifyType Mobile = new(2, LoginTypeConsts.Mobile, "mobile", "captcha", "手机号或验证码错误");

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="id">枚举类Id</param>
        /// <param name="name">枚举类名称</param>
        /// <param name="accountField">账号字段</param>
        /// <param name="passwordField">密码字段</param>
        /// <param name="errorMessage">基本错误消息</param>
        public LoginVerifyType(int id, string name, string accountField, string passwordField, string errorMessage)
            : base(id, name)
        {
            this.AccountField = accountField;
            this.PasswordField = passwordField;
            this.ErrorMessage = errorMessage;
        }

        /// <summary>
        /// 账号字段
        /// </summary>
        public string AccountField { get; private set; }
        /// <summary>
        /// 密码字段
        /// </summary>
        public string PasswordField { get; private set; }
        /// <summary>
        /// 标准错误消息
        /// </summary>
        public string ErrorMessage { get; private set; }

        /// <summary>
        /// 获取普通账号或关联的手机号
        /// </summary>
        /// <param name="values"></param>
        /// <returns></returns>
        public string GetAccount(LoginParams values)
        {
            if (values.TryGetValue(AccountField, out object account))
            {
                return Convert.ToString(account);
            }
            return null;
        }

        /// <summary>
        /// 获取密码或验证码
        /// </summary>
        /// <param name="values"></param>
        /// <returns></returns>
        public string GetPassword(LoginParams values)
        {
            if (values.TryGetValue(PasswordField, out object password))
            {
                return Convert.ToString(password);
            }
            return null;
        }
    }
}
